package network

import (
	"gitee.com/amoyx/cloud-provider/pkg/cloud/tencent/models"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	tvpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
)

type SubnetGreeter interface {
	Subnets(region string) SubnetInterface
}

type SubnetInterface interface {
	Create(request models.SubnetRequest) (id string)
	Get(id string) (result models.SubnetResponse)
	Delete(id string) bool
	ListByVpcId(vpcId string) (result []models.SubnetResponse)
	List() (list []models.SubnetResponse)
}

type subnet struct {
	network *Network
	region  string
	client  *tvpc.Client
}

func newSubnet(network *Network, region string) SubnetInterface {
	v := &subnet{
		network: network,
		region:  region,
	}
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = string(VpcEndPoint)
	client, _ := tvpc.NewClient(v.network.Credential(), v.region, cpf)
	v.client = client
	return v
}

func (s *subnet) Create(in models.SubnetRequest) (id string) {
	request := tvpc.NewCreateSubnetRequest()
	request.VpcId = common.StringPtr(in.VpcId)
	request.SubnetName = common.StringPtr(in.SubnetName)
	request.CidrBlock = common.StringPtr(in.CidrBlock)
	request.Zone = common.StringPtr(in.Zone)
	var tags []*tvpc.Tag
	for key, val := range in.Tags {
		tags = append(tags, &tvpc.Tag{
			Key:   common.StringPtr(key),
			Value: common.StringPtr(val),
		})
	}
	request.Tags = tags
	response, err := s.client.CreateSubnet(request)
	if err != nil {
		return
	}
	id = *response.Response.Subnet.SubnetId
	return
}

func (s *subnet) Delete(id string) bool {
	request := tvpc.NewDeleteSubnetRequest()
	request.SubnetId = common.StringPtr(id)
	_, err := s.client.DeleteSubnet(request)
	if err != nil {
		return false
	}
	return true
}

func (s *subnet) Update() {

}

func (s *subnet) Get(id string) (result models.SubnetResponse) {
	list := s.query(map[string]string{"subnet-id": id})
	if len(list) >= 1 {
		return list[0]
	}
	return
}

func (s *subnet) Resource(id string) (list []models.SubnetResourceResponse) {
	request := tvpc.NewDescribeSubnetResourceDashboardRequest()
	request.SubnetIds = common.StringPtrs([]string{id})
	response, err := s.client.DescribeSubnetResourceDashboard(request)
	if err != nil {
		return
	}
	if len(response.Response.ResourceStatisticsSet) < 1 {
		return
	}
	for _, val := range response.Response.ResourceStatisticsSet[0].ResourceStatisticsItemSet {
		list = append(list, models.SubnetResourceResponse{
			ResourceType:  *val.ResourceType,
			ResourceName:  *val.ResourceName,
			ResourceCount: *val.ResourceCount,
		})
	}
	return
}

func (s *subnet) ListByVpcId(vpcId string) (list []models.SubnetResponse) {
	list = s.query(map[string]string{"vpc-id": vpcId})
	return
}

// List 获取所有subnet数据
func (s *subnet) List() (list []models.SubnetResponse) {
	return s.query(map[string]string{})
}

// query 查询 subnet子网信息
func (s *subnet) query(args map[string]string) (list []models.SubnetResponse) {
	request := tvpc.NewDescribeSubnetsRequest()
	var filters []*tvpc.Filter
	for k, v := range args {
		filters = append(filters, &tvpc.Filter{
			Name:   common.StringPtr(k),
			Values: common.StringPtrs([]string{v}),
		})
	}
	request.Filters = filters
	request.Limit = common.StringPtr("100")
	response, err := s.client.DescribeSubnets(request)
	if err != nil {
		return
	}
	for _, v := range response.Response.SubnetSet {
		list = append(list, models.SubnetResponse{
			VpcId:                   *v.VpcId,
			SubnetId:                *v.SubnetId,
			SubnetName:              *v.SubnetName,
			CidrBlock:               *v.CidrBlock,
			IsDefault:               *v.IsDefault,
			Zone:                    *v.Zone,
			RouteTableId:            *v.RouteTableId,
			CreatedTime:             *v.CreatedTime,
			AvailableIpAddressCount: *v.AvailableIpAddressCount,
			NetworkAclId:            *v.NetworkAclId,
			IsRemoteVpcSnat:         *v.IsRemoteVpcSnat,
			TotalIpAddressCount:     *v.TotalIpAddressCount,
		})
	}
	return
}
